 /* 
 * File:   cSentinel.cpp
 * Author: josep
 * 
 * Created on 27 / desembre / 2010, 13:10
 */

#include "cSentinel.h"

cSentinel::cSentinel():cAutonomousAgent() {
    ObjetctType=SENTINEL_TYPE;
}

cSentinel::cSentinel(const cSentinel& orig):cAutonomousAgent(orig) {
}

cSentinel::~cSentinel() {
}

void cSentinel::Init(cTexture* texture, int PosX, int PosY, int Width, int Height){
    cAutonomousAgent::Init(texture, PosX, PosY, Width, Height);
    cSentinel::Init();
}

void cSentinel::Init(){
    FromLogicToViewCorrespondence.resize(5);
    FromLogicToViewCorrespondence[0]=1;
    FromLogicToViewCorrespondence[1]=3;
    FromLogicToViewCorrespondence[2]=2;
    FromLogicToViewCorrespondence[3]=4;
    FromLogicToViewCorrespondence[4]=5;

    StateMachine<cSentinel> *vSm = new StateMachine<cSentinel>();

    vSm->addState(this);
    vSm->addState(this);
    vSm->addState(this);
    vSm->addState(this);
    vSm->getState(OFF)->setName("Off");
    vSm->getState(MOVING_LEFT)->setName("Moving to left");
    vSm->getState(MOVING_RIGHT)->setName("Moving to right");
    vSm->getState(DEATH)->setName("Death");

    vSm->getState(OFF)->setTransition(TO_LEFT, vSm->getState(MOVING_LEFT));
    vSm->getState(OFF)->setTransition(TO_RIGHT, vSm->getState(MOVING_RIGHT));

    vSm->getState(MOVING_LEFT)->setTransition(DIE, vSm->getState(DEATH));
    vSm->getState(MOVING_LEFT)->setTransition(TO_RIGHT, vSm->getState(MOVING_RIGHT));

    vSm->getState(MOVING_RIGHT)->setTransition(DIE, vSm->getState(DEATH));
    vSm->getState(MOVING_RIGHT)->setTransition(TO_LEFT, vSm->getState(MOVING_LEFT));

    vSm->getState(MOVING_LEFT)->addSubscription(State<cSentinel>::SUBSCRIPTION_IN,
                                                   &cSentinel::ChangeFramestate);
    vSm->getState(MOVING_RIGHT)->addSubscription(State<cSentinel>::SUBSCRIPTION_IN,
                                                   &cSentinel::ChangeFramestate);
    vSm->getState(DEATH)->addSubscription(State<cSentinel>::SUBSCRIPTION_IN,
                                                   &cSentinel::ChangeFramestate);
    vSm->setInitialState(OFF);

    sm = (StateMachine<cLateralAgent> *)vSm;
}

void cSentinel::Notify(cPhysicalReaction *reaction){
    if(reaction->GetReactionType()==cPhysicalReaction::COLLISION){
        if(reaction->GetReactionEvent()==cCollisionEvent::EAST_COLLISION){
            GoLeft();
        }else if(reaction->GetReactionEvent()==cCollisionEvent::WEST_COLLISION){
            GoRight();
        }
    }
    cAutonomousAgent::Notify(reaction);
}

bool cSentinel::NotifyAgentCollision(cAgentCollisionEvent* revent){
	bool ret = true;
    if(cAutonomousAgent::NotifyAgentCollision(revent)){
        if(revent->GetArea()->hasIncluded(revent->GetReactionEvent(), c2DArea::INCLUDE_EAST)){
        //if(GetArea()->hasIncluded(revent->GetReactionEvent(), c2DArea::INCLUDE_EAST)){
            GoLeft();
			ret=false;
        }else if(revent->GetArea()->hasIncluded(revent->GetReactionEvent(), c2DArea::INCLUDE_WEST)){
        //}else if(GetArea()->hasIncluded(revent->GetReactionEvent(), c2DArea::INCLUDE_WEST)){
            GoRight();
			ret=false;
        }
    }
	return ret;
}



